package Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

//行动监听
public class MyJfram extends JFrame implements ActionListener {
    //创建一个按钮对象
    JButton jb1 = new JButton("点");
    //创建第二个按钮对象
    JButton jb2=new JButton("再");
    public MyJfram(){
        //设置界面大小
        this.setSize(600,650);
        //设置界面标题
        this.setTitle("拼图");
        //设置页面默认置顶
        this.setAlwaysOnTop(true);
        //设置页面默认居中
        this.setLocationRelativeTo(null);
        //设置默认关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消内置默认居中设置
        this.setLayout(null);


        //设置按钮1的位置和宽高

        jb1.setBounds(0,0,50,20);
        //给按钮添加事件
        jb1.addActionListener(this);
        //设置按钮2的位置和宽高
        jb2.setBounds(50,0,50,20);
        jb2.addActionListener(this);
        //将按钮添加到页面
        this.getContentPane().add(jb1);
        this.getContentPane().add(jb2);

        //可视化
        this.setVisible(true);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object sou = e.getSource();
        if (sou == jb1){
            jb1.setSize(40,15);
        } else if (sou == jb2) {
            Random r = new Random();
            int ran = r.nextInt(600);
            jb2.setLocation(ran,ran);
        }
    }
}
